package cn.juque.workflow.service.provider;

import cn.hutool.core.bean.BeanUtil;
import cn.juque.common.annotation.HandlerAppException;
import cn.juque.common.base.BaseResponseDTO;
import cn.juque.common.base.PageInfo;
import cn.juque.common.constants.MessageEnum;
import cn.juque.common.thread.BaseOperatorThreadLocal;
import cn.juque.common.utils.ValidatorUtil;
import cn.juque.workflow.api.dto.processinfo.ProcessDeployParamDTO;
import cn.juque.workflow.api.dto.processinfo.ProcessInfoListDTO;
import cn.juque.workflow.api.dto.processinfo.ProcessInfoSearchDTO;
import cn.juque.workflow.api.dto.taskinfo.ProcessInfoDeleteDTO;
import cn.juque.workflow.api.provider.IProcessInfoProvider;
import cn.juque.workflow.entity.ProcessInfo;
import cn.juque.workflow.service.IProcessInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author JUQUE
 * @version 1.0.0
 * <ul>
 *  <li>simple-generator</li>
 *  <li>流程信息</li>
 * </ul>
 * @date 2023-01-27 15:45:40
 **/
@Slf4j
@Service("processInfoProvider")
@HandlerAppException
public class ProcessInfoProviderImpl implements IProcessInfoProvider {

    @Resource
    private BaseOperatorThreadLocal baseOperatorThreadLocal;

    @Resource
    private IProcessInfoService processInfoService;

    @Resource
    private ValidatorUtil validatorUtil;

    /**
     * 列表检索
     *
     * @param searchDTO 入参
     * @return List
     */
    @Override
    public BaseResponseDTO<List<ProcessInfoListDTO>> list(ProcessInfoSearchDTO searchDTO) {
        this.validatorUtil.checkNotNull(searchDTO);
        PageInfo pageInfo = searchDTO.getPageInfo();
        ProcessInfo processInfo = new ProcessInfo();
        BeanUtil.copyProperties(searchDTO, processInfo);
        List<ProcessInfo> infoList;
        if (null == pageInfo) {
            infoList = this.processInfoService.listInfoWithoutPage(processInfo);
        } else {
            infoList = this.processInfoService.listInfoWithPage(pageInfo, processInfo);
        }
        List<ProcessInfoListDTO> infoDtoList = BeanUtil.copyToList(infoList, ProcessInfoListDTO.class);
        return new BaseResponseDTO<>(MessageEnum.OK, infoDtoList, pageInfo);
    }

    /**
     * 部署流程
     *
     * @param paramDTO 参数
     * @return String
     */
    @Override
    public BaseResponseDTO<String> deploy(ProcessDeployParamDTO paramDTO) {
        this.validatorUtil.fastValidate(paramDTO);
        this.baseOperatorThreadLocal.set(paramDTO.getBaseOperator());
        String deployId = this.processInfoService.saveInfo(paramDTO.getFileName(), paramDTO.getContent());
        return new BaseResponseDTO<>(MessageEnum.OK, deployId);
    }

    /**
     * 删除
     *
     * @param req 入参
     * @return Boolean
     */
    @Override
    public BaseResponseDTO<Boolean> delete(ProcessInfoDeleteDTO req) {
        List<String> infoIdList = req.getIdList();
        this.processInfoService.deleteInfo(infoIdList);
        return new BaseResponseDTO<>(MessageEnum.OK, Boolean.TRUE);
    }
}
